package com.cashmama.market.api.domain.service.impl;

import com.alibaba.fastjson.JSON;
import com.cashmama.market.api.domain.service.ApiMessageService;
import com.cashmama.market.api.infras.exception.ApplicationException;
import com.fqgj.msg.push.PushMsgService;
import com.fqgj.msg.ro.PushMsgConditionRequestRO;
import com.fqgj.msg.ro.PushMsgInfoRO;
import com.fqgj.msg.ro.SingleSmsSendRequestRO;
import com.fqgj.msg.sms.SmsMsgService;
import com.fqgj.msg.utils.Paged;
import com.fqgj.msg.utils.Result;
import com.google.common.collect.Maps;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @program: market-api
 * @description
 * @author: frank
 * @create: 2019-12-20 15:40
 **/
@Service
public class MessageIntegrationServiceImpl implements ApiMessageService {

    private static final Logger LOGGER = LoggerFactory.getLogger(MessageIntegrationServiceImpl.class);

    @Autowired
    private SmsMsgService smsMsgService;

    @Autowired
    private PushMsgService pushMsgService;

    @Override
    public Boolean sendMessage(String phone, String bizCode, String verifyCode) {

        SingleSmsSendRequestRO requestRO = new SingleSmsSendRequestRO();
        requestRO.setPhone(phone);
        requestRO.setType(1);
        requestRO.setBizCode(bizCode);
        Map<String, String> replaceParam = Maps.newHashMap();
        replaceParam.put("VERIFY_CODE", verifyCode);
        requestRO.setReplaceParam(replaceParam);
        Result<Boolean> result = smsMsgService.send(requestRO);
        LOGGER.info("send verifyCode by new messageCenter,bizCode:" + bizCode + ",phone:" + phone + ",code:" + verifyCode);
        if (result == null || !result.isSuccess() || result.getData() == null) {
            LOGGER.error("send verifyCode failed ,phone:{}", phone);
            throw new ApplicationException("send verifyCode failed");
        }
        return result.getData();
    }

    @Override
    public boolean getUnReadPushMsgNum(String userCode) {
        try {
            Result<Integer> unReadPushMsgNum = pushMsgService.getUnReadPushMsgNum(userCode);
            if (unReadPushMsgNum.isSuccess() && unReadPushMsgNum.getData() > 0) {
                return Boolean.TRUE;
            } else {
                return Boolean.FALSE;
            }
        } catch (Exception e) {
            LOGGER.error("调用message项目失败,userCode:{}", userCode, e);
            return false;
        }
    }

    @Override
    public List<PushMsgInfoRO> queryPushMsgByCondition(PushMsgConditionRequestRO pushMsgConditionRequestRO) {
        try {
            Result<Paged<List<PushMsgInfoRO>>> result = pushMsgService.queryPushMsgByCondition(pushMsgConditionRequestRO);
            if (!result.isSuccess()) {
                return new ArrayList<>();
            }
            return result.getData().getData();
        } catch (Exception e) {
            LOGGER.error("调用message项目失败,pushMsgConditionRequestRO:{}", JSON.toJSONString(pushMsgConditionRequestRO), e);
            return new ArrayList<>();
        }
    }
}
